// *************************************************************************
//
//                                JOST
//
//               Copyright (C) 2007 by Angelo Corsaro.
//                       <angelo.corsaro@gmail.com>
//                          All Rights Reserved.
//
//   Permission to use, copy, modify, and distribute this software and
//   its  documentation for any purpose is hereby  granted without fee,
//   provided that the above copyright notice appear in all copies and
//   that both that copyright notice and this permission notice appear
//   in  supporting  documentation. I don't make  any  representations
//   about the  suitability  of this  software for any  purpose. It is
//   provided "as is" without express or implied warranty.
//
//
// *************************************************************************
//  
// *************************************************************************
#ifndef _JOST_VALUE_H_
#define _JOST_VALUE_H_

#include "jost/Exceptions.hpp"

namespace jost {
  class Value;
}


/**
 * The class <code>Value</code> provides the common abstraction for
 * JSON types, and thus values.<br>
 *
 * JOST <b>Memory Management</b>, assumes that <code>Value</code>s
 * contained in aggregated elements such as <code>Object</code> or
 * <code>Array</code> are owned by their container object. This means
 * that when the container object, the contained elements are relased
 * as well.<br>
 *
 * @author Angelo Corsaro <mailto:angelo.corsaro@gmail.com>
 * @version 0.1
 */ 
class  jost::Value 
{  
public:
    /**
     * JSON Supported Types.
     */
    enum TypeID {
	ARRAY, 
	BOOLEAN,
	INTEGER,
	NIL, /* This implementation treats nil as an empty object */
	OBJECT,
	REAL,
	STRING,
	VALUE
    };
    
    /**
     * Value Dtor.
     */
    virtual ~Value() throw();
  
  /**
   * Returns the type ID for this <code>Value</code> instance.
   *
   * @return The <code>TypeID</code> associated with the runtime
   * instance referenced.
   */
  inline TypeID getTypeID() const throw ();
    
public:
  /**
   * Provides a stringified version of the Object which is compliant
   * with the JSON format.
   *
   * @return a stringfied representation of the value.
   */
  virtual std::string    serialize() throw() = 0;
    
protected:
  Value(TypeID typeID) throw();

protected:
  TypeID typeID_;
};

namespace jost {
    template <typename T> T*
    extract(Value* v) throw(jost::IllegalTypeConversion) 
    {			
        T* retVal = dynamic_cast<T*>(v);
	if (retVal == 0)	
            throw jost::IllegalTypeConversion("Illegal type conversion attempted!");	
	return retVal;
    }
}	

#include "jost/Value.inl"
#endif /*_JOST_VALUE_H_*/
